/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.core.parser.util;

import com.farmcode.api.runtime.regex.Match;
import com.farmcode.api.runtime.regex.MatchContext;
import com.farmcode.api.runtime.regex.MatchContextCollection;
import java.io.Serializable;
import java.util.regex.Pattern;

/**
 *
 * @author oiprado
 */
public class Function implements Serializable {

    public static final String CASE_PATTERN = "^_|^\\s|^\\*|^\\!|^\\@|^\\#|^\\$|^\\%|^\\^|^\\&|^\\(|^\\)|^\\-|^\\+|^\\=|^\\?|^\\<|^\\>";
    private Match match;

    public Function(Match match) {
        this.match = match;
    }

    public String camelCase(String string) {
        string = match.replace(string);
        StringBuilder stringBuilder = new StringBuilder();
        char[] inputCharArr = string.toCharArray();
        int length = inputCharArr.length;
        boolean beforeSpecialCharacter = false;
        Pattern pattern = java.util.regex.Pattern.compile(CASE_PATTERN);
        for (int index = 0; index < length; index++) {
            String character = "" + inputCharArr[index];
            if (pattern.matcher(character).find()) {
                beforeSpecialCharacter = true;
                continue;
            }

            if (beforeSpecialCharacter || index == 0) {
                character = character.toUpperCase();
            } else {
                character = character.toLowerCase();
            }

            stringBuilder.append(character);
            beforeSpecialCharacter = false;
        }

        return stringBuilder.toString();

    }

    public static String removeString(String string, String input) {
        return input.replace(string, "");
    }

    public String mixedCase(String string) {
        string = match.replace(string);
        StringBuilder stringBuilder = new StringBuilder();
        char[] inputCharArr = string.toCharArray();
        int length = inputCharArr.length;
        boolean beforeSpecialCharacter = false;
        Pattern pattern = java.util.regex.Pattern.compile(CASE_PATTERN);
        for (int index = 0; index < length; index++) {
            String character = "" + inputCharArr[index];
            if (pattern.matcher(character).find()) {
                beforeSpecialCharacter = true;
                continue;
            }

            if (beforeSpecialCharacter) {
                character = character.toUpperCase();
            } else {
                character = character.toLowerCase();
            }

            stringBuilder.append(character);
            beforeSpecialCharacter = false;
        }

        return stringBuilder.toString();
    }
}
